package com.meteor.downloader.videodownloader.admdm

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import coil.load
import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdLoader
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.LoadAdError
import com.google.android.gms.ads.nativead.NativeAdOptions
import com.google.android.gms.ads.nativead.NativeAdView
import com.meteor.downloader.videodownloader.MdmDealApplication
import com.meteor.downloader.videodownloader.R

object BookNativeUtils {
    fun loadTIAdInfo(valueOriInfo: ValueMdmInfo, end: (Boolean) -> Unit) {
        if (valueOriInfo.mdmAdID.isEmpty()) {
            end.invoke(false)
            return
        }
        if (!valueOriInfo.mBookLoading) {
            valueOriInfo.mBookLoading = true
            val adLoader = AdLoader.Builder(MdmDealApplication.mMdmContext, valueOriInfo.mdmAdID)
                .forNativeAd { ad ->
                    valueOriInfo.mNativeObject = ad
                    valueOriInfo.mBookLoading = false
                    valueOriInfo.mLoadTime = System.currentTimeMillis()
                    end.invoke(true)
                }.withAdListener(object : AdListener() {
                    override fun onAdFailedToLoad(p0: LoadAdError) {
                        valueOriInfo.mBookLoading = false
                        valueOriInfo.changeResultToNull()
                        end.invoke(false)
                    }

                    override fun onAdImpression() {
                        super.onAdImpression()
                        val mNativeAd = valueOriInfo.mNativeObject
                        mNativeAd?.let {
                            it.setOnPaidEventListener { it2 ->
                                AdInfoUtils.adValueUpload(MdmDealApplication.mMdmContext, it2)
                                AdInfoUtils.setAdSeMdmNative(it2, it, valueOriInfo.mdmAdID)
                            }
                        }
                        valueOriInfo.changeResultToNull()
                        MdmDealAdUtils.loadBookNativeMdm(false)
                    }
                }).withNativeAdOptions(NativeAdOptions.Builder().build()).build()
            adLoader.loadAd(
                AdRequest.Builder().build()
            )
        } else {
            end.invoke(false)
        }
    }

    /**
     * nativeType = "one","two"
     */
    fun showMdmAdValue(
        context: Context,
        nativeType: String,
        valueOriInfo: ValueMdmInfo,
        listener: ShowPageAdListener
    ) {
        val mNativeAd = valueOriInfo.mNativeObject ?: run {
            listener.nativeAddShowBack(null, null)
            return
        }
        val adView = when (nativeType) {
            "sizeSmall" -> {
                View.inflate(context, R.layout.layout_ad_size_small_ui, null)
            }

            "sizeCenter" -> {
                View.inflate(context, R.layout.layout_ad_size_center_ui, null)
            }

            else -> {
                View.inflate(context, R.layout.layout_ad_size_center_ui, null)
            }
        } as NativeAdView

        adView.iconView = adView.findViewById(R.id.dvsNativeIcon)
        (adView.iconView as ImageView).load(mNativeAd.icon?.drawable)
        adView.headlineView = adView.findViewById(R.id.dvsNativeTitle)
        (adView.headlineView as TextView).text = mNativeAd.headline

        if (nativeType != "sizeSmall") {
            adView.mediaView = adView.findViewById(R.id.nativeMediaView)
            if (mNativeAd.mediaContent != null) {
                adView.mediaView?.visibility = View.VISIBLE
                adView.mediaView?.mediaContent = mNativeAd.mediaContent
            } else {
                adView.mediaView?.visibility = View.GONE
            }
        }

        if (nativeType == "sizeSmall" || nativeType == "sizeBig") {
            adView.bodyView = adView.findViewById(R.id.dvsNativeInfo)
            (adView.bodyView as TextView).text = mNativeAd.body
        }

        adView.callToActionView = adView.findViewById(R.id.tiNativeButton)
        (adView.callToActionView as TextView).text = mNativeAd.callToAction

        adView.setNativeAd(mNativeAd)
        listener.nativeAddShowBack(adView, mNativeAd)
    }

}